package com.westvalley.workflow.action.hr;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.action.BasisWe8Action;
import com.westvalley.common.action.result.ActionResult;
import com.westvalley.common.action.util.WorkFlowActionUtil;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.result.JsonResult;
import com.westvalley.service.api.hr.InsertShouldRemoveSerivce;
import com.westvalley.service.api.hr.bean.TransferReady;

import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.RequestInfo;

/**
 * 调动预申请
 * @author 莫双
 *
 */
public class D001CreateAction extends BasisWe8Action{

	@Override
	protected ActionResult executeResult(RequestInfo requestInfo) {
		ActionResult result = ActionResult.me(requestInfo);
		JsonResult jsonResult = InsertShouldRemoveSerivce.me(initBean(requestInfo)).getJsonResult();
		if(!jsonResult.isFlag())
			throw new RuntimeException(jsonResult.getMsg());
		else{
			//更新HR单据编号
			JSONObject resultJson = (JSONObject) jsonResult.getResult();
			Db.update("UPDATE "+mainTableName+" SET hrDocCode = ? WHERE requestId = ?", resultJson.getString("sheetId"),requestInfo.getRequestid());
		}
		return result;
	}
	
	private TransferReady initBean(RequestInfo requestInfo){
		MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
		TransferReady bean = new TransferReady();
		//数据源id(调出店)
		Record recordSubcompany = Db.findFirst("SELECT * FROM HrmSubCompany WHERE ID = ?", WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaordh111"));	
		if(recordSubcompany!=null)
			bean.setShopId(StringKit.null2o(recordSubcompany.get("subcompanycode")));	
		//OA单据编号
		bean.setSheetId(WorkFlowActionUtil.getFieldValue(mainTableInfo, "danjbh"));			
		//操作人
		Record recordPsn = Db.findFirst("SELECT * FROM HRMRESOURCE WHERE ID = ?", WorkFlowActionUtil.getFieldValue(mainTableInfo, "shenqr"));	
		if(recordPsn!=null)
			bean.setOperator(StringKit.null2o(recordPsn.get("workcode")));		
		//员工编号
		Record recordId = Db.findFirst("SELECT * FROM HRMRESOURCE WHERE ID = ?", WorkFlowActionUtil.getFieldValue(mainTableInfo, "yuangxm"));	
		if(recordId!=null)
			bean.setId(StringKit.null2o(recordId.get("workcode")));		
		//调动原因
		bean.setNote(WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaodyy"));
		//调出公司
		if(recordId!=null){
			Record recordSubCompany = Db.findFirst("SELECT * FROM HrmSubCompany WHERE ID = ?", StringKit.null2o(recordId.get("subcompanyid1")));	
			if(recordSubCompany!=null)
				bean.setOutShopId(StringKit.null2o(recordSubCompany.get("subcompanycode")));
		}
		//调出部门
		if(recordId!=null){
			Record recordDept = Db.findFirst("SELECT * FROM HrmDepartment WHERE ID = ?", StringKit.null2o(recordId.get("departmentid")));	
			if(recordDept!=null)
				bean.setOutOfficeId(StringKit.null2o(recordDept.get("departmentcode")));
		}
		//调出职务
		if(recordId!=null){
			Record recordJob = Db.findFirst("SELECT * FROM HrmJobTitles WHERE ID = ?", StringKit.null2o(recordId.get("jobtitle")));	
			if(recordJob!=null)
				bean.setOutDutyId(StringKit.null2o(recordJob.get("jobtitlecode")));
		}
		//调入公司
		if(recordSubcompany!=null)
			bean.setInShopId(StringKit.null2o(recordSubcompany.get("subcompanycode")));
		//调入部门
		Record recordDept = Db.findFirst("SELECT * FROM HrmDepartment WHERE ID = ?", WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaorbmbh111"));	
		if(recordDept!=null)
			bean.setInOfficeId(StringKit.null2o(recordDept.get("departmentcode")));
		//调入职务
		Record recordJob = Db.findFirst("SELECT * FROM HrmJobTitles WHERE ID = ?", WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaorzwbh111"));	
		if(recordJob!=null)
			bean.setInDutyId(StringKit.null2o(recordJob.get("jobtitlecode")));
		//调入日期
		bean.setMoveDate(WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaodrq"));
		//报到日期
		bean.setInDate(WorkFlowActionUtil.getFieldValue(mainTableInfo, "baodrq"));
		//调动类型
		bean.setMoveType(WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaodlx"));
		//调入业务岗位
		Record recordJobBusiness = Db.findFirst("SELECT * FROM HrmJobTitles WHERE ID = ?", WorkFlowActionUtil.getFieldValue(mainTableInfo, "yewgwbh111"));
		if(recordJobBusiness!=null)
			bean.setInPartid(StringKit.null2o(recordJobBusiness.get("jobtitlecode")));
		//调动标志
		bean.setMoveFlag(WorkFlowActionUtil.getFieldValue(mainTableInfo, "diaodbz"));
		return bean;
	}
	
}
